package com.lzm.listendemo.view.like;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.RelativeLayout;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class LikeContainer extends RelativeLayout implements LikeView.AnimatorListener {
    private static final String TAG = "LikeContainer";
    private long lastClickTime;
    private Context mContext;
    private int currentNumber;

    public LikeContainer(Context context) {
        this(context, null);
    }

    public LikeContainer(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LikeContainer(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
    }


    private void addThumbImage(Context context, float x, float y, LikeView.AnimatorListener animatorListener) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 8; i++) {
            list.add(i);
        }
        Collections.shuffle(list);//打乱顺序
        for (int i = 0; i < 5; i++) {
            LayoutParams layoutParams = new LayoutParams(100, 100);
            layoutParams.setMargins((int) x, (int) y - 50, 0, 0);
            LikeView articleThumb = new LikeView(context);
            articleThumb.setType(list.get(i));
            articleThumb.setAnimatorListener(animatorListener);
            if (getChildCount() > 1)
                this.addView(articleThumb, getChildCount() - 1, layoutParams);
            else {
                this.addView(articleThumb, layoutParams);
            }
        }
    }

    public LikeContainer(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public void setThumb(float x, float y, LikeContainer articleThumbRl) {
        if (System.currentTimeMillis() - lastClickTime > 800) {//单次点击
            addThumbImage(mContext, x, y, this);
            lastClickTime = System.currentTimeMillis();
            for (int i = getChildCount() - 5; i < getChildCount(); i++) {
                if (getChildAt(i) instanceof LikeView) {
                    ((LikeView) getChildAt(i)).setThumb(true, articleThumbRl);
                }
            }
            currentNumber = 0;

        } else {//连续点击
            lastClickTime = System.currentTimeMillis();
            addThumbImage(mContext, x, y, this);
            for (int i = getChildCount() - 5; i < getChildCount(); i++) {
                if (getChildAt(i) instanceof LikeView) {
                    ((LikeView) getChildAt(i)).setThumb(true, articleThumbRl);
                }
            }
            currentNumber++;
        }
    }

    @Override
    public void onAnimationEnd() {
        super.onAnimationEnd();
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {

            }
        }, 10);
    }
}
